import { UserService } from './../../providers/UserService';
import { BasePage } from './../basePage';
import { StorageService } from './../../providers/StorageService';
import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

/**
 * Generated class for the ChoseAddressPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-chose-address',
  templateUrl: 'chose-address.html',
})
export class ChoseAddressPage extends BasePage {

  public userinfo : any;
  public addressArr=[0,1,2,3,4,5];


  constructor(
   
    public inject:Injector,
    public userService:UserService,
    public stc:StorageService
    ) {
      super(inject);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ChoseAddressPage');
  }

  /*每次进来的时候都要获取地址*/
  ionViewWillEnter(){
    this.userinfo = this.stc.read('userinfo');
    if(this.userinfo == null){
      this.showAlert("请先登录")
    }
    /*获取 全部的收货地址*/
    this.getUserAddress();
  }

  /*获取用户的默认的收货地址*/
  async getUserAddress(){

    let loader = this.loadingCtrl.create({
      content: "",
    });
    loader.present();

    let result = await this.userService.getAddressListAPI({user_id:this.userinfo.user_id});
    loader.dismiss();
    console.log(result.data);
    /*选出是默认地址的*/
    let addressArr = result.data.filter((item)=>{
     return item.default_address == '1'
    });
    /*选出不是默认地址的*/
    let addressArr2 = result.data.filter((item)=>{
      return item.default_address == '0';
    })
    this.addressArr = addressArr.concat(addressArr2);
    console.log(this.addressArr);
   
  }
  /*跳转到 订单确认页面*/
  toOrderPage(item){
    console.log("跳转到 订单页面");
    /*将收货地址的信息 保存在本地缓存中*/
    // console.log(item);
    this.stc.write('shopAddress',item);
    this.navCtrl.pop();
  }
  toManagerGoods(){
    console.log("跳转到地址修改页面");
    this.navCtrl.push('ManagergoodsPage',{isFromOrder:true});
  }


}
